import os
import subprocess
import time
t1 = time.time()
# 获取当前目录
current_path = os.getcwd()

# # 设置可执行文件的输出路径
output_file = os.path.join(current_path, 'c_find_longest_palindrome.exe')



# 检查编译是否成功
def run():
    # 编译C代码
    compile_process = subprocess.run(["gcc", "-O3", "-o", output_file, "c_find_longest_palindrome.c"])
    if compile_process.returncode != 0:return

    print("编译成功，正在运行程序...")

    # 运行可执行文件
    try:
        run_process = subprocess.run(output_file)
        if run_process.returncode == 0:
            print("程序运行成功。")
        else:
            print("程序运行出错。")
    except Exception as e:
        print(f"运行时发生错误: {e}")

    ######## 删除可执行文件
    try:
        os.remove(output_file)
        print(f"已删除可执行文件: {output_file}")
    except Exception as e:
        print(f"删除文件时发生错误: {e}")

run()
print(time.time()-t1)